/**
 * 
 */
package actions.accounts;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import tools.hibernate.HibernateUtils;
import models.accounts.GBILogin;
import models.accounts.GBLogin;
import models.accounts.entity.GBPassport;

import java.sql.*;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.GBHibernateDAOImpl;

/**
 * @author asua
 * 
 */
public class GBLoginAction extends ActionSupport implements ModelDriven<GBPassport> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(GBLoginAction.class
			.getName());
	public GBPassport userPassport =new GBPassport();

	public GBPassport getUserPassport() {
		return userPassport;
	}

	public void setUserPassport(GBPassport userPassport) {
		this.userPassport = userPassport;
	}

	public String execute() throws SQLException {
		GBILogin login=(GBILogin) new ClassPathXmlApplicationContext("spring.xml").getBean("login_tool");
		login.init();
		if(login.CheckLogin()||login.isLogin()){
			logger.debug("already logined in");
			return SUCCESS;
		}
		if(userPassport.getUserName()!=null){
			login.Login(userPassport.getUserName(), userPassport.getPassword(),true); 
			if(login.isLogin()){
				return SUCCESS;
			}else{
				return ERROR;
			}
		}else{
			return INPUT;
		}
	}

	public GBPassport getModel() {
		return userPassport;
	}
}
